#!/bin/sh

# checksum tests to verify that captured audio is correct.

BASEDIR="`dirname "${0}"`/.."
. "${BASEDIR}/functions"


. "${BASEDIR}/extractaudio/common.sub"

TKINDS="mono stereo"
OFMTS_mono="wav raw w64"
OFMTS_stereo="wav w64"

for tkind in ${TKINDS}
do
  if [ ${tkind} = "mono" ]
  then
    EXTRACTAUDIO_ARGS=""
    OFMTS="${OFMTS_mono}"
    CHFMT="m"
  else
    EXTRACTAUDIO_ARGS="-s"
    OFMTS="${OFMTS_stereo}"
    CHFMT="s"
  fi
  for rfile in ${extractaudio_RFILES}
  do
    bfile="`basename ${rfile}`"
    ofile="extractaudio/${bfile}.output"
    for ofmt in ${OFMTS}
    do
      DFMTS=`cat extractaudio/extractaudio.${CHFMT}.${ofmt}.dfmts`
      for dfmt in ${DFMTS}
      do
        wfile="${bfile}.${tkind}.${dfmt}.${ofmt}"
        sfile="${bfile}.${tkind}.${dfmt}.${ofmt}.tout"
        logfile="${bfile}.${tkind}.${dfmt}.${ofmt}.rlog"
        if [ -e "${wfile}" ]
        then
          rm "${wfile}"
        fi
        afile="extractaudio/${bfile}.args"
        if [ -e "${afile}" ]
        then
          EXTRACTAUDIO_EARGS=`"${afile}" "${rfile}"`
        else
          EXTRACTAUDIO_EARGS="${rfile}"
        fi
        ${EXTRACTAUDIO} -F ${ofmt} -D ${dfmt} ${EXTRACTAUDIO_ARGS} \
         ${EXTRACTAUDIO_EARGS} ${wfile} 2>${logfile} >${sfile}
        report "${EXTRACTAUDIO} ${rfile}"
        test -s ${wfile}
        report "${rfile} -> ${wfile}"
        sha256_verify ${wfile} extractaudio/extractaudio.checksums
        ${DIFF} "${ofile}" "${sfile}"
        report "checking RTP stats for the ${rfile}"
      done
    done
  done
done
